//
//  AdTypeActionsCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 9/1/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class AdTypeActionsCell: UITableViewCell {
    
    private lazy var primaryBtn: UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = 15
        btn.backgroundColor = .blueColor
        btn.setTitleColor(.white, for: .normal)
        return btn
    }()
    
    private lazy var secondaryBtn: UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = 15
        btn.backgroundColor = .lightBlueColor
        btn.setTitleColor(.greyColor, for: .normal)
        btn.setTitle("Не публиковать сразу", for: .normal)
        return btn
    }()
    
    
    @objc private func primaryClick (sender: UIButton) {
        if sender.tag == 1 {
            delegate.freePublish()
        } else {
            delegate.buyVip()
        }
    }
    @objc private func secondaryClick (sender: UIButton) {
        delegate.publishLater()
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addViews(isFree: reuseIdentifier == "freeAction")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var delegate: AdTypeControllerDelegate!
    
    let priceCount = 3999
    
    private func updateUI(isFree: Bool) {
        if isFree {
            self.contentView.addSubview(secondaryBtn)
            contentView.backgroundColor = .whiteBlueColor
            secondaryBtn.snp.makeConstraints { (make) in
                make.height.equalTo(44)
                make.leading.trailing.equalToSuperview().inset(16)
                make.top.equalTo(primaryBtn.snp.bottom).offset(20)
                make.bottom.equalToSuperview().offset(-20)
            }
            primaryBtn.setTitle("Разместить бесплатно", for: .normal )
            primaryBtn.addTarget(self, action: #selector(primaryClick(sender:)), for: .touchUpInside)
            primaryBtn.tag = 1 // FREE
            secondaryBtn.addTarget(self, action: #selector(secondaryClick(sender:)), for: .touchUpInside)
            
        } else {
            contentView.backgroundColor = .mediumGreyBlue
            primaryBtn.setTitle("Разместить за \(priceCount)₸ на 60 дней", for: .normal )
            primaryBtn.addTarget(self, action: #selector(primaryClick(sender:)), for: .touchUpInside)
            primaryBtn.tag = 2 // VIP
            secondaryBtn.removeFromSuperview()
            primaryBtn.snp.makeConstraints { (make) in
                make.bottom.equalToSuperview().offset(-20)
            }
        }
    }
    
    private func addViews (isFree: Bool) {
        self.contentView.addSubview(primaryBtn)
        primaryBtn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(20)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(44)
        }
        self.updateUI(isFree: isFree)
    }
}
